package group.sicher.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;

/**
 * Created by bootvue@gmail.com
 * Date 2019-04-04 12:13
 */
@Slf4j
public class EmailUtil {
    /**
     * 文本消息发送, 支持html内容
     *
     * @param sender
     * @param address 接收方地址集合
     * @param subject 标题
     * @param content 邮件内容
     * @param html    是否是html内容
     * @return
     */
    public static boolean send(JavaMailSender sender, String[] address, String subject, String content, boolean html) {
        boolean flag = false;
        try {
            MimeMessage message = sender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("notification@sicher.group");
            helper.setSubject(subject);
            helper.setTo(address);
            helper.setText(content, html);
            //发送,走起
            sender.send(message);
            flag = true;
            log.info("{} 邮件发送成功", address);
        } catch (Exception e) {
            log.error("邮件发送失败: ", e);
        }
        return flag;
    }

    /**
     * 普通文本消息发送
     *
     * @param sender
     * @param address 接收方地址集合
     * @param subject 标题
     * @param content 邮件内容
     * @return
     */
    public static boolean send(JavaMailSender sender, String[] address, String subject, String content) {
        return send(sender, address, subject, content, false);
    }

    /**
     * html文本消息发送
     *
     * @param sender
     * @param address 接收方地址集合
     * @param subject 标题
     * @param content 邮件内容
     * @return
     */
    public static boolean sendHtml(JavaMailSender sender, String[] address, String subject, String content) {
        return send(sender, address, subject, content, true);
    }
}
